package org.ii.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.ii.domain.user.User;
import org.springframework.transaction.annotation.Transactional;

public class UserDaoImpl implements UserDao{
		
	private EntityManager entityManager;
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;
	}

	@Transactional
	public void createUser(User user) {
		entityManager.persist(user);		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getAllUserNames() {
		return entityManager.createQuery("Select user.name from User user").getResultList();
	}

	@Override
	public Long getUserCount() {
		return (Long) entityManager.createQuery("Select count(user.id) from User user").getSingleResult();
	}

}
